<template>
  <div style="min-width: 200px;height: 100%"  />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null,
      chartConfig:{
        xAxis: {
          data: null,
          boundaryGap: false,
          axisTick: {
            show: false
          },
        },
        yAxis: {
          axisTick: {
            show: false
          },
          splitLine: {
            show: false
          }
        },
        grid: {
          left: 10,
          right: 10,
          bottom: 20,
          top: 30,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10]
        },
        legend: {
          data: [],
          textStyle: {
            color: '#ffffff'  // 设置图例文字为白色
          }
        },
        series: [

          ]
      }
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    },
    setOptions(obj) {

      this.chartConfig.xAxis.data = obj.x

      obj.dataTag.forEach(item=>{
        this.chartConfig.legend.data.push(item.name)
        this.chartConfig.series.push({
          name: item.name,
          itemStyle: {
            normal: {
              color: item.color,
              lineStyle: {
                color: item.color,
                width: 2
              }
            }
          },
          smooth: true,
          type: 'line',
          data: obj.data[item.tag],
          animationDuration: 2800,
          animationEasing: 'cubicInOut'
        })


      })





      this.chart.setOption(this.chartConfig)
    }
  }
}
</script>
